<?php
///
///  composer 包
///  "maatwebsite/excel": "^3.1",
///
///

namespace App\Excel;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithMappedCells;


class CheckinRewardImport implements ToCollection, WithMappedCells
{

    public function mapping(): array
    {
        return [];
    }

    public function rules(): array
    {

    }

    public function collection(Collection $rows)
    {
        // TODO: Implement collection() method.

//        \Log::info($rows);

//        die;
    }

}


namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;
use App\Models\CheckinReward;
use App\Models\Checkin;
use App\Models\ContinuityCheckin;
use App\Models\ContinuityCheckinReward;
use App\Excel\CheckinRewardImport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;


class CheckinAdminController extends BaseAdminController
{
    public $where = [];

    /**
     * excel导入
     */
    public function import(Request $request)
    {
        $file = $request->file('excel');
        if (empty($file)) {
            return $this->apiFail('请上传文件');
        }
        $type = $request->post('type');
        if (empty($type)) {
            return $this->apiFail('请选择类型');
        }
        $path = 'storage/uploads';
        //移动文件
        $fileName = $file->getClientOriginalName();
        $file->move($path, $fileName);
        if (!file_exists($path . '/' . $fileName)) {
            return $this->apiFail('文件读取失败');
        }
        $CheckinRewardImport = Excel::toArray(new CheckinRewardImport(), $path . '/' . $fileName);
        if (empty($CheckinRewardImport)) {
            return $this->apiFail('文件读取失败');
        }

        $CheckinRewardImport = $CheckinRewardImport[0];

        foreach ($CheckinRewardImport as $key => &$val) {
            $val['day'] = $val[0];
            $val['reward_type'] = $val[1];
            $val['reward_number'] = $val[2];
            unset($CheckinRewardImport[$key][0]);
            unset($CheckinRewardImport[$key][1]);
            unset($CheckinRewardImport[$key][2]);
        }
        $msg = [];

        return $this->apiSuccess('导入完成', $msg);
    }
}

